import request from './request'

export function createQuestion(content, answered_by) {
  return request({
    url: '/question/questions/',
    method: 'post',
    data: {
      content,
      answered_by
    }
  })
}

export function loadQuestionsByAnswer(userId, answered) {
  return request({
    url: `/question/questions/?asked_by=${userId}&answer__isnull=${!answered}&ordering=-datetime`,
    method: 'get'
  })
}

export function loadQuestionsToAnswer(userId, answered) {
  return request({
    url: `/question/questions/?answered_by=${userId}&answer__isnull=${!answered}&audited_by__isnull=false&ordering=-datetime`,
    method: 'get'
  })
}

export function loadQuestionsToAudited() {
  return request({
    url: `/question/questions/?answer__isnull=true&audited_by__isnull=true&rejected_by__isnull=true&ordering=-datetime`,
    method: 'get'
  })
}

export function loadQuestionsRejected(userId) {
  return request({
    url: `/question/questions/?asked_by=${userId}&rejected_by__isnull=false&ordering=-datetime`,
    method: 'get'
  })
}

export function auditQuestion(questionId) {
  return request({
    url: `/question/questions/${questionId}/audit/`,
    method: 'patch'
  })
}

export function rejectQuestion(questionId) {
  return request({
    url: `/question/questions/${questionId}/reject/`,
    method: 'patch'
  })
}

export function answerQuestion(questionId, answer) {
  return request({
    url: `/question/questions/${questionId}/answer/`,
    method: 'patch',
    data: {
      answer
    }
  })
}

export function editQuestion(questionId, content) {
  return request({
    url: `/question/questions/${questionId}/edit/`,
    method: 'patch',
    data: {
      content
    }
  })
}

export function deleteQuestion(questionId) {
  return request({
    url: `/question/questions/${questionId}/`,
    method: 'delete'
  })
}
